<?php

namespace Admin\Controller;



class UserController extends AdminController {



    #列表

    public function index(){ 

        $role = M('Role')->getField('id,name');

        $obj = M('Admin');

        $list = $obj->order('id asc')->select();

        $this->assign('list', $list);   

        $this->assign('role', $role);    

    	$this->display();

    }



    #添加

    public function add(){

        $obj = M('Role');

        $this->assign('group',$obj->select());

    	$this->display();

    }



    #保存

    public function save(){

        $obj = D('Admin');



        if ($data = $obj->create()) {

            if ($list=$obj->add($data)) {



                $obj = M('roleUser');

                $tmp['role_id']=$data['group'];

                $tmp['user_id']=$list;

                $obj->add($tmp);



                $state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('User/index');

            } else {

                $state = 'Operation failed';

            }

        } else {

            $state = $obj->getError();

        }

        echo $this->echo_json_str($state,$message,$url);

    }



    #编辑

    public function edit(){

        $id = (int) I('get.id');

        if (!isset ($id)) {

            $this->error('Parameter error');

        }

        $obj = M('Admin');

        $list = $obj->where('id=' . $id)->find();

        $this->assign('list', $list);

        $obj = M('Role');

        $this->assign('group',$obj->select());

        $this->display();

    }



    #更新

    public function update(){



        $obj = D('Admin');



        if ($data = $obj->create()) {

            if($data['password']==''){

                unset($data['password']);

            }else{

                $data['password']=md5($data['password']);

            }

            if ($obj->save($data)) {



                $role = M('roleUser');

                $role->where('user_id='.$data['id'])->setField('role_id',$data['group']);



                $state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('User/index');

            } else {

                $state = 'Operation failed';

            }

        } else {

            $state = $obj->getError();

        }

        echo $this->echo_json_str($state,$message,$url);

    }



    //删除

    public function del() {

        $id = $_GET['id'];

        if (!isset ($id)) {

            echo '0';

        }



        $obj = M('Admin');

        $list = $obj->where('id=' . $id)->delete();

        

        $access = M('AdminLog');

        $access->where('uid=' . $id)->delete();



        $roleUser=M('roleUser');

        $roleUser->where('user_id='.$id)->delete();



        if(!$list){

            echo '0';

        }else{

            echo '1';

        }

    }



    //管理员登陆日志

    public function log(){



        $uid = $_GET['id'];

        if($uid!=''){

            $map['uid']=$uid;

        }

        $log = M('AdminLog');        

        $count = $log->where($map)->count();

        import("Common.ORG.Page");

        $page = new \Page($count, 20);

        $show = $page->show();

        $list = $log->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

        foreach ($list as $key => $value) {

            $list[$key]['username'] = M('Admin')->where('id='.$value['uid'])->getField('username');

        }

        $this->assign('list', $list);

        $this->assign('page', $show);

        $this->display();

    }



    //删除日志

    public function delog(){



        $id = $_POST['selectedids'];

        if(!isset($id)){

            $state = "You do not select any information!";

        }else{

            $where['id']=array('in',$id);

            $log=M('adminLog');

            $list = $log->where($where)->delete();

            if($list){

                $state = 'SUCCESS';

                $message = 'Successful operation';

                $url = U('User/log',array('id'=>I('post.userID')));

            }else{

                $state = "Operation failed";

            }

        }        

        echo $this->echo_json_str($state,$message,$url);

    }



    //修改密码

    public function password(){

        if(isset($_POST['dosubmit'])) {

            $oldpassword = I('post.oldpwd');

            $password = I('post.password');

            $condition['id']=$this->adminID;

            $obj = M('Admin');

            $userpwd = $obj->where($condition)->field('password')->find();

            if($userpwd['password']!=md5($oldpassword)){

                $state = 'Original password error';

            }else{

                $data['password']=md5($password);

                $obj->where($condition)->save($data);

                $state = 'SUCCESS';

                $message = 'Password modification success';

                $url = U('Index/index');

            }

            echo $this->echo_json_str($state,$message,$url);            

        }else{

            $this->display();

        }        

    }

}